﻿
using Awsc.DataAcquisitionSys.Core.Databases;
using Microsoft.EntityFrameworkCore;

namespace Awsc.DataAcquisitionSys.Api.StartupFilters
{
    public class DatabaseInitializationStartupFilter : IStartupFilter
    {
        public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
        {
            return async app =>
            {
                // 在中间件管道配置前创建数据库
                using var scope = app.ApplicationServices.CreateScope();
                var dbContext = scope.ServiceProvider.GetRequiredService<AppDbContext>();
                dbContext.Database.Migrate(); // 或 Migrate() 用于迁移

                next(app); // 继续配置后续中间件

            };
        }
    }
}
